package com.orifine.sql.chain;

import com.orifine.orm.model.ColumnModel;
import com.orifine.sql.SyntaxString;

public class ChainOn implements ChainSql {
	private ChainOnType type;
	private String column1Name;
	private String column2Name;
	
	private ChainOn(ChainOnType type, ColumnModel column1, ColumnModel column2){
		this.type = type;
		this.column1Name = column1.getTable().getName() + "." + column1.getName();
		this.column2Name = column2.getTable().getName() + "." + column2.getName();
	}
	
	@Override
	public void chain(SyntaxString out) {
		out.addWord("ON");
		switch(type){
		case EQ:
			out.addWord(column1Name).append("=").append(column2Name);
			break;
		case GE:
			out.addWord(column1Name).append(">=").append(column2Name);
			break;
		case GT:
			out.addWord(column1Name).append(">").append(column2Name);
			break;
		case LE:
			out.addWord(column1Name).append("<=").append(column2Name);
			break;
		case LT:
			out.addWord(column1Name).append("<").append(column2Name);
			break;
		case UEQ:
			out.addWord(column1Name).append("<>").append(column2Name);
			break;
		}
	}

	@Override
	public String sql() {
		SyntaxString syntaxString = new SyntaxString();
		chain(syntaxString);
		return syntaxString.toString();
	}
	
	/**
	 * 大于
	 */
	public static ChainOn gt(ColumnModel column1, ColumnModel column2){
		return new ChainOn(ChainOnType.GT, column1, column2);
	}
	
	/**
	 * 小于
	 */
	public static ChainOn lt(ColumnModel column1, ColumnModel column2){
		return new ChainOn(ChainOnType.LT, column1, column2);
	}
	
	/**
	 * 等于
	 */
	public static ChainOn eq(ColumnModel column1, ColumnModel column2){
		return new ChainOn(ChainOnType.EQ, column1, column2);
	}
	
	/**
	 * 大于或等于
	 */
	public static ChainOn ge(ColumnModel column1, ColumnModel column2){
		return new ChainOn(ChainOnType.GE, column1, column2);
	}
	
	/**
	 * 小于或等于
	 */
	public static ChainOn le(ColumnModel column1, ColumnModel column2){
		return new ChainOn(ChainOnType.LE, column1, column2);
	}
	
	/**
	 * 不等于
	 */
	public static ChainOn ueq(ColumnModel column1, ColumnModel column2){
		return new ChainOn(ChainOnType.UEQ, column1, column2);
	}
}
